package com.wing.order.controller.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.order.model.entity.DistributionDemandDetail;
import com.wing.order.model.response.DistributionDemandDetailVO;
import com.wing.order.service.DistributionDemandDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.order.model.request.DistributionDemandDetailForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-配货需求计划明细表管理", tags = "配货需求计划明细表管理")
@RestController("appDistributionDemandDetailController")
@RequestMapping("order/app/distributionDemandDetail")
public class DistributionDemandDetailController {

    @Autowired
    private DistributionDemandDetailService distributionDemandDetailService;
    @Autowired
    private LocaleMessage localeMessage;

    @ApiOperation(value = "领货详情列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "demandId", value = "配货需求单id", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<DistributionDemandDetailVO> list(
            @RequestParam(name = "page",  defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit",  defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort",  defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order", defaultValue = "desc", required = false) String order
            ,Long demandId
    ) {
        Page<DistributionDemandDetailVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("demandId",demandId);
        Page<DistributionDemandDetailVO> list = distributionDemandDetailService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/detail/{id}")
    public JsonResult detail(@PathVariable("id") Long distributionDemandDetailId) {
        DistributionDemandDetailVO distributionDemandDetailVO = distributionDemandDetailService.get4appById(distributionDemandDetailId);
        if (distributionDemandDetailVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), distributionDemandDetailVO);
    }

    @PostMapping()
    public JsonResult add(@Validated DistributionDemandDetailForm distributionDemandDetailForm) {
        if (distributionDemandDetailService.add(distributionDemandDetailForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

}
